/* 
 * File:   Walker.cpp
 * Author: paula
 * 
 * Created on 14 de noviembre de 2014, 14:39
 */

#include <Box2D/Common/b2Math.h>

#include "Walker.h"

Walker::Walker() {
}

Walker::Walker(int x, int y, SDL_Renderer** gRenderer, int character) {

    this->directionRight = true;
    this->counterWalk = 1;
    this->counterWalkII = 1;
    this->counterKneel = 1;
    this->counterJump = 1;
    this->counterWait = 9;
    this->beginning = true;
    this->rot = 0;
    this->jumping = false;
    this->kneeling = false;
    this->walking = false;
    this->fallingDown = false;
    this->wasJumping = false;
    this->initConsFall = 3;
    this->constFall = initConsFall;

    this->gRenderer = *gRenderer;
    this->dot = new Dot(x, y);
    this->path = "Img/" + to_string(character);
    
    this->imageToDraw = this->principalR;

    this->loadMedia();
}

Walker::Walker(const Walker& orig) {
}

void Walker::draw() {
    changeImage(this->imageToDraw, this->rot);
}

void Walker::actualizePosition(float posX, float posY) {
    this->dot->setPos(posX, posY);
    this->dot->dotWorldToPixels();
}

int Walker::getState() {
    return this->state;
}

int Walker::getCharacterId() {
    return this->data.idCharacter;
}

struct ObjectInit Walker::getData() {
    return this->data;
}

void Walker::updateData(struct ObjectInit data2) {

    this->dot->setPos(data2.x, data2.y);
    this->data = data2;
    
//    cout << "PLAYERRRR " << this->data.idCharacter << " state " <<this->data.stateCharacter<<endl;
}

void Walker::changeImage(SBSImage* image, float rot) {
    SDL_Rect* principalSettings = new SDL_Rect();
    b2Vec2* dotito = new b2Vec2();
    dotito->x = this->getPosX();
    dotito->y = this->getPosY();
//    cout << "dir image " << image << endl;
    principalSettings->w = (image->getWidth()); //agregar escala
    principalSettings->h = (image->getHeight()); //agregar escala
    principalSettings->x = dotito->x;
    principalSettings->y = dotito->y;
    principalSettings->x = principalSettings->x - (principalSettings->w / 2);
    principalSettings->y = principalSettings->y - (principalSettings->h / 2);
    
//   cout << "id " << this->data.idCharacter << "    principalSettings->x " <<     principalSettings->x<< endl;   
//   cout << "pathhhhh " << image->path << endl;
//    cout << "    principalSettings->y " <<     principalSettings->y<< endl;
//    cout << "    principalSettings->w " <<     principalSettings->w<< endl;
//    cout << "rndeeer " << gRenderer << endl;
    SDL_RendererFlip flip;
    if (directionRight) {
        flip = SDL_FLIP_NONE;
    } else {
        flip = SDL_FLIP_HORIZONTAL;
    }
    SDL_RenderCopyEx(gRenderer, image->getTexture(), NULL, principalSettings, rot, NULL, flip);
    
//      cout << "aaaid " << this->data.idCharacter << "    principalSettings->x " <<     principalSettings->x<< endl;   
//   
//    cout << "   aaaa principalSettings->y " <<     principalSettings->y<< endl;
//    cout << "  aaaa  principalSettings->w " <<     principalSettings->w<< endl;
    delete principalSettings;
    delete dotito;
}

bool Walker::isWalkingRight() {
    if (this->walking && this->directionRight) {
        return true;
    }
    return false;
}

bool Walker::isWalkingLeft() {
    if (this->walking && !(this->directionRight)) {
        return true;
    }
    return false;
}

void Walker::keepFallingDown() {
    //    changeImage(principalUR1, rot);
    this->imageToDraw = principalUR1;
}

void Walker::kneel() {

    this->walking = false;
    beginning = false;
    kneeling = true;
    //    changeImage(principalDR1, rot);
    this->imageToDraw = principalDR1;
    counterKneel++;
    SDL_Delay(20);
}

void Walker::keepKneeling() {
    switch (this->counterKneel) {
        case (1):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            counterKneel++;
            break;
        case (2):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            counterKneel++;
            break;
        case (3):
            //            changeImage(principalDR2, rot);
            this->imageToDraw = principalDR2;
            counterKneel++;
            break;
        case (4):
            //            changeImage(principalDR3, rot);
            this->imageToDraw = principalDR3;
            counterKneel++;
            break;
        case (5):
            //            changeImage(principalDR2, rot);
            this->imageToDraw = principalDR2;
            counterKneel++;
            break;
        case (6):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            break;
    }
//    SDL_Delay(20);
}

void Walker::stopKneeling() {
    counterKneel = 1;
    this->kneeling = false;
}

void Walker::jump() {
    this->beginning = false;
    jumping = true;
    kneeling = false;
    counterKneel = 1;
    if (counterJump = 1) {
        //        changeImage(principalDR1, rot);
        this->imageToDraw = principalDR1;
    }
    counterJump++;
}

void Walker::keepJumping() {

    this->beginning = false;
    jumping = true;
    kneeling = false;
    counterKneel = 1;
    switch (this->counterJump) {
        case (1):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            counterJump++;
            break;
        case (2):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            counterJump++;
            break;
        case (3):
            //            changeImage(principalDR2, rot);
            this->imageToDraw = principalDR2;
            counterJump++;
            break;
        case (4):
            //            changeImage(principalDR3, rot);
            this->imageToDraw = principalDR3;
            counterJump++;
            break;
        case (5):
            //            changeImage(principalDR2, rot);
            this->imageToDraw = principalDR2;
            counterJump++;
            break;
        case (6):
            //            changeImage(principalDR1, rot);
            this->imageToDraw = principalDR1;
            counterJump++;
            break;
        case (7):
            //            changeImage(principalUR1, rot);
            this->imageToDraw = principalUR1;
            counterJump++;
            break;
        case (8):
            //            changeImage(principalUR2, rot);
            this->imageToDraw = principalUR2;
            counterJump++;
            break;
        case (9):
            //            changeImage(principalUR3, rot);
            this->imageToDraw = principalUR3;
            jumping = false;
            wasJumping = true;
            break;
    }
}

void Walker::wait() {
    this->walking = false;
    kneeling = false;
    counterKneel = 1;
    if (this->isFallingDown()) {
        this->keepFallingDown();
    } else {
        if (counterWait > 8) {
            //            changeImage(principalR, rot);
            this->imageToDraw = principalR;
            velocityWalkRight = 0.0;
            velocityWalkLeft = 0.0;
        } else {
            //            changeImage(principalR1, rot);
            this->imageToDraw = principalR1;
            counterWait++;
        }
        if (counterWait > 100) {
            counterWait = 9;
        }
    }
}

void Walker::keepWalking() {
    switch (this->counterWalk) {
        case (1):
            //            changeImage(principalR0, rot);
            this->imageToDraw = principalR0;
            break;
        case (2):
            //            changeImage(principalR1, rot);
            this->imageToDraw = principalR1;
            break;
        case (3):
            //            changeImage(principalR2, rot);
            this->imageToDraw = principalR2;
            break;
        case (4):
            //            changeImage(principalR3, rot);
            this->imageToDraw = principalR3;
            break;
        case (5):
            //            changeImage(principalR4, rot);
            this->imageToDraw = principalR4;
            break;
        case (6):
            //            changeImage(principalR3, rot);
            this->imageToDraw = principalR3;
            break;
        case (7):
            //            changeImage(principalR2, rot);
            this->imageToDraw = principalR2;
            break;
    }
    if ((counterWalkII % 6) == 1) {
        counterWalk++;
    }
    counterWalkII++;
    if (counterWalk == 8) {
        counterWalk = 2;
    }
    if (counterWalkII > 1000) {
        counterWalkII = 1;
    }
}

void Walker::walkRight() {
    kneeling = false;
    counterKneel = 1;
    counterWait = 1;
    this->beginning = false;
    //    changeImage(principalR0, rot);
    this->imageToDraw = principalR0;
    this->directionRight = true;
    this->walking = true;
    counterWalkII++;
}

void Walker::walkLeft() {
    kneeling = false;
    counterKneel = 1;
    counterWait = 1;
    this->beginning = false;
    this->walking = true;
    //    changeImage(principalR0, rot);
    this->imageToDraw = principalR0;
    this->directionRight = false;
    counterWalkII++;
}

bool Walker::isJumping() {
    return this->jumping;
}

bool Walker::isWalking() {
    return this->walking;
}

bool Walker::isKneeling() {
    return this->kneeling;
}

void Walker::stopWalking() {
    this->walking = false;
}

bool Walker::isFallingDown() {

    return this->fallingDown;
}

void Walker::setJumping() {
    this->jumping = true;
    this->kneeling = false;
    this->fallingDown = false;
    this->walking = false;
    this->jumping = false;
    this->waiting = false;
}

void Walker::setKneeling() {
    this->jumping = false;
    this->fallingDown = false;
    this->walking = false;
    this->jumping = false;
    this->waiting = false;
    this->kneeling = true;
    counterJump = 1;
}

void Walker::setFalling() {
    this->jumping = false;
    this->kneeling = false;
    this->walking = false;
    this->jumping = false;
    this->waiting = false;
    this->fallingDown = true;
    counterJump = 1;

}

void Walker::setWalking() {
    this->jumping = false;
    this->kneeling = false;
    this->fallingDown = false;
    this->jumping = false;
    this->waiting = false;
    this->walking = true;
    counterJump = 1;
}

void Walker::setWaiting() {
    this->jumping = false;
    this->kneeling = false;
    this->fallingDown = false;
    this->walking = false;
    this->jumping = false;
    counterJump = 1;
    this->waiting = true;
}

bool Walker::getDirectionRight() {
    return this->directionRight;
}

void Walker::setDirectionRight(bool direction) {
    this->directionRight = direction;
}

float Walker::getPosX() {
    return this->dot->getPos()->x;
}

float Walker::getPosY() {
    return this->dot->getPos()->y;
}

void Walker::loadMedia() {

    principalR = new SBSImage();
    principalR0 = new SBSImage();
    principalR1 = new SBSImage();
    principalR2 = new SBSImage();
    principalR3 = new SBSImage();
    principalR4 = new SBSImage();
    principalDR1 = new SBSImage();
    principalDR2 = new SBSImage();
    principalDR3 = new SBSImage();
    principalUR1 = new SBSImage();
    principalUR2 = new SBSImage();
    principalUR3 = new SBSImage();

    principalR->loadMedia(this->path + "/r.png", &gRenderer);
    principalR0->loadMedia(this->path + "/r0.png", &gRenderer);
    principalR1->loadMedia(this->path + "/r1.png", &gRenderer);
    principalR2->loadMedia(this->path + "/r2.png", &gRenderer);
    principalR3->loadMedia(this->path + "/r3.png", &gRenderer);
    principalR4->loadMedia(this->path + "/r4.png", &gRenderer);
    principalDR1->loadMedia(this->path + "/dR1.png", &gRenderer);
    principalDR2->loadMedia(this->path + "/dR2.png", &gRenderer);
    principalDR3->loadMedia(this->path + "/dR3.png", &gRenderer);
    principalUR1->loadMedia(this->path + "/uR1.png", &gRenderer);
    principalUR2->loadMedia(this->path + "/uR2.png", &gRenderer);
    principalUR3->loadMedia(this->path + "/uR3.png", &gRenderer);

}

void Walker::freeMedia() {

    delete principalR;
    delete principalR0;
    delete principalR1;
    delete principalR2;
    delete principalR3;
    delete principalR4;
    delete principalDR1;
    delete principalDR2;
    delete principalDR3;
    delete principalUR1;
    delete principalUR2;
    delete principalUR3;

}

Walker::~Walker() {
    //    delete this->dot;
    //    this->freeMedia();
}

